﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Enums;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.MiniApp;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsDotCloudCollectRepository : BaseRepository<MsDotCloudCollect>, IMsDotCloudCollectRepository
    {
        public IQueryable<CollectAndUpload> QueryUploadPoint(Guid taskId)
        {
            var query = from c in Db.MsDotCloudCollect
                        join u in Db.DotCloudUpload on new { c.TaskId, c.PointIndex } equals new { u.TaskId, u.PointIndex } into fu
                        from lu in fu.DefaultIfEmpty()
                        where c.TaskId == taskId
                        select new CollectAndUpload(c) { UploadStatus = lu.Status, UploadErrorMsg = lu.Msg };
            return query.AsNoTracking();
        }

        public async ValueTask<List<CollectAndUpload>> QueryNeedUploadPointListAsync(Guid[] ids)
        {
            if (ids.IsEmpty())
                return new List<CollectAndUpload>();

            var query = from c in Db.MsDotCloudCollect
                        join u in Db.DotCloudUpload on new { c.TaskId, c.PointIndex } equals new { u.TaskId, u.PointIndex } into fu
                        from lu in fu.DefaultIfEmpty()
                        where ids.Contains(c.TaskId)
                        select new CollectAndUpload(c) { UploadStatus = lu.Status, UploadErrorMsg = lu.Msg };
            return await query.Where(p => p.UploadStatus != UploadStatusEnum.上传成功).AsNoTracking().ToListAsync();
        }
    }
}